import csv
import traceback
import os
from Operate.BusinessOperate import BusinessOperate


class UtilsFile:

    def __init__(self, filePath):
        self.filePath = filePath

    # 写文件
    def write(self, fileDate):
        try:
            csvFile = open(self.filePath, 'a', encoding='utf-8', newline='')
            writer = csv.writer(csvFile)
            writer.writerows(fileDate)
        except BaseException as e:
            msg = traceback.format_exc()
            print(msg)
        finally:
            csvFile.close()

    # 读取文件
    def read(self):
        csvFile = open(self.filePath, encoding='utf-8')
        try:
            return csvFile.readlines()
        finally:
            csvFile.close()

    # 数据驱动读取csv的数据
    def get_csv_data(filePath):
        if "testCase" in os.getcwd():
            dataPath = "../../Data/"
        else:
            dataPath = "./Data"
        value_rows = []
        with open(dataPath + filePath, encoding='utf-8') as f:
            # 忽略表头
            f_csv = csv.reader(f)
            next(f_csv)
            for r in f_csv:
                value_rows.append(r)
        return value_rows


if __name__ == '__main__':
    utilsFile = UtilsFile("../data/data.csv")
    fileData = []
    for i in range(100):
        fileData.append(
            [BusinessOperate.getChinsesName(), BusinessOperate.getEmail(), BusinessOperate.getMobilePhone()])
        fileData.append(["姓名", "邮箱", "手机号码"])
        utilsFile.write(fileData)

    for line in utilsFile.read():
        print(line)
